/**
 * 创建对象的方式
 */


//(1)工厂模式 : 封装函数

function createPerson(name, age, job) {
  //1.原料
  var o = new Object();
  //2.加工
  o.name = name;
  o.age = age;
  o.job = job;
  o.sayName = function () {
    console.log(this.name)
  };
  //3.出场
  return o;
}

var p1 = createPerson('linqinghu', 19, 'job');
p1.sayName()

//(2)构造函数模式：

function Person(name, age, job) {
  this.name = name;
  this.age = age;
  this.job = job;
  this.sayName = function () {
    console.log(this.name);
  }
}

//当new去调用一个函数 : 这个时候函数中的this就是创建出来的对象,而且函数的的返回值直接就是this啦(隐式返回)
var p2 = new Person('linqinghu2', 12, 'job');
p2.sayName();

//(3)字面量模式

var o = {
  name: "yangyule",
  age: 23,
  sex: "male",
  init: function () {
    console.log(this.name)
  }
};
o.init()

//(4)原型模式

function Person2() {

}

Person2.prototype.name = '林庆虎';
Person2.prototype.age = 18;
Person2.prototype.sayName = function () {
  console.log(this.name);
};

var person2 = new Person2();
person2.sayName();

//(5)Object构造函数
var hh = new Object();

hh.name = 'linqinghu';
hh.age = 23;
hh.job = 'lllll';
hh.sayName = function () {
  console.log(this.name);
};
hh.sayName();

//6. 组合使用构造函数和原型模式

function Person3(name, age, sex) {
  this.name = name;
  this.age = age;
  this.sex = sex;
}

Person3.prototype.sayName = function () {
  console.log(this.name);
};
var person3 = new Person3('linqinghu3', 23, 'lll');
var person4 = new Person3('linqinghu3', 23, 'llll');
console.log(person3.sayName() === person4.sayName());//true 都在一个同一原型上
person3.sayName();










